﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WPFSample.PrintWithMarginsSample
{
    class PrintWithMargins : Window
    {
        PrintQueue prnqueue;
        PrintTicket prntkt;
        Thickness marginPage = new Thickness(96);

        public PrintWithMargins()
        {
            this.Title = "Print with Margins";
            this.FontSize = 24;

            StackPanel stack = new StackPanel();
            this.Content = stack;

            Button btn = new Button
            {
                Content = "Page Set_up...",
                HorizontalAlignment = HorizontalAlignment.Center,
                Margin = new Thickness(24)
            };
            btn.Click += this.SetupOnClick;
            stack.Children.Add(btn);

            btn = new Button()
            {
                Content = "_Print...",
                HorizontalAlignment = HorizontalAlignment.Center,
                Margin = new Thickness(24)
            };
            btn.Click += this.PrintOnClick;
            stack.Children.Add(btn);
        }

        private void PrintOnClick(object sender, RoutedEventArgs e)
        {
            PrintDialog dlg = new PrintDialog();
            if (this.prnqueue != null)
            {
                dlg.PrintQueue = this.prnqueue;
            }

            if (this.prntkt != null)
            {
                dlg.PrintTicket = this.prntkt;
            }

            if (dlg.ShowDialog().GetValueOrDefault())
            {
                this.prnqueue = dlg.PrintQueue;
                this.prntkt = dlg.PrintTicket;

                DrawingVisual vis = new DrawingVisual();
                DrawingContext dc = vis.RenderOpen();
                Pen pn = new Pen(Brushes.Black, 1);

                Rect rectPage = new Rect(
                    this.marginPage.Left, 
                    this.marginPage.Top, 
                    dlg.PrintableAreaWidth - (this.marginPage.Left + this.marginPage.Right), 
                    dlg.PrintableAreaHeight - (this.marginPage.Top + this.marginPage.Bottom));

                dc.DrawRectangle(null, pn, rectPage);

                FormattedText formtxt = new FormattedText(
                    string.Format("Hello, Printer! {0} x {1}", dlg.PrintableAreaWidth, dlg.PrintableAreaHeight),
                    CultureInfo.CurrentCulture,
                    FlowDirection.LeftToRight,
                    new Typeface(new FontFamily("Times New Roman"),
                    FontStyles.Italic, FontWeights.Normal,
                    FontStretches.Normal),
                    48, Brushes.Black);
                Size sizeText = new Size(formtxt.Width, formtxt.Height);

                Point ptText = new Point(rectPage.Left + (rectPage.Width - formtxt.Width) / 2,
                    rectPage.Top + (rectPage.Height - formtxt.Height) / 2);

                dc.DrawText(formtxt, ptText);
                dc.DrawRectangle(null, pn, new Rect(ptText, sizeText));
                dc.Close();

                dlg.PrintVisual(vis, this.Title);
            }
        }

        private void SetupOnClick(object sender, RoutedEventArgs e)
        {
            PageMarginsDialog dlg = new PageMarginsDialog()
            {
                Owner = this,
                PageMargins = this.marginPage
            };
            if (dlg.ShowDialog().GetValueOrDefault())
            {
                this.marginPage = dlg.PageMargins;
            }
        }
    }
}
